package com.attendance.chdingtask.service;

import com.attendance.chdingtask.dao.BaseJPA;
import com.attendance.chdingtask.utils.common.LambdaQuery;
import com.attendance.chdingtask.utils.common.QueryDslTool;
import com.attendance.chdingtask.utils.exception.BizException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public abstract class BaseService <T,K extends Serializable> {
    @Autowired
    protected LambdaQuery lambdaQuery;
    @Autowired
    EntityManager entityManager;
    @Autowired
    public QueryDslTool queryDslTool;
    protected abstract BaseJPA<T> getDao();
    public T findById(Long id) {
        Optional<T> byId = getDao().findById(id);
        if (!byId.isPresent()){
            return  null;
        }
        return getDao().findById(id).get();
    }

    public List<T> findAll() {
        return getDao().findAll();
    }

    @Transactional
    public T save(T entity)  {
        return  getDao().save(entity);
    }

    @Transactional
    public void saveAll(List<T> list) {
        getDao().saveAll(list);
    }

    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    public T update(Long id,Object obj){
        Optional<T> byId = getDao().findById(id);
        if (!byId.isPresent()){
            return  null;
        }
        T one = byId.get();
        BeanUtils.copyProperties(obj,one);
        T save = getDao().save(one);
        return save;
    }

    @Transactional
    public void delete(Long id) {
        getDao().deleteById(id);
    }

    public void deleteAll(List<T> list) {
        getDao().deleteInBatch(list);
    }

    public void errorException(String msg){
        throw new BizException(msg);
    }


    public void checkStringEmpty(String s , String errMsg){
        if (s == null || "".equals(s)){
            errorException(errMsg);
        }
    }

    public void checkObjNull(Object o , String errMsg){
        if (o == null){
            errorException(errMsg);
        }
    }

    public void checkMoenyZero(BigDecimal bigDecimal , String errMsg){
        if (bigDecimal == null || BigDecimal.valueOf(0).equals(bigDecimal)){
            errorException(errMsg);
        }
    }

    protected void errorMsg(String msg) {
        throw new BizException(msg);
    }

    protected void checkString(String str, String msg) {
        checkNull(str, msg);
        if(str.trim().isEmpty()) {
            throw new BizException(msg);
        }
    }
    /**
     * 判断是否为空
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        if (obj instanceof List){
            if (checkList((List)obj)){
                return true;
            }
        }
        if (obj instanceof Map){
            if (checkMap((Map)obj)){
                return true;
            }
        }
        if (obj == null || obj.equals("")) {
            return true;
        }

        return false;
    }
    public static void checkNull(Object obj, String msg) {
        if (obj == null) {
            throw new BizException(msg);
        }
        if (obj instanceof List){
            if (checkList((List)obj)){
                throw new BizException(msg);
            }
        }
        if (obj instanceof Map){
            if (checkMap((Map)obj)){
                throw new BizException(msg);
            }
        }
    }
    public static Boolean checkList(List list) {
        if (list==null||list.size()<=0) {
            return  true;
        }
        return  false;
    }
    public static Boolean checkMap(Map map) {
        if (map==null||map.size()<=0) {
            return  true;
        }
        return  false;
    }
}
